/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package test4.menus;

import de.lessvoid.nifty.Nifty;
import de.lessvoid.nifty.controls.dynamic.LabelCreator;
import de.lessvoid.nifty.controls.dynamic.PanelCreator;
import de.lessvoid.nifty.controls.dynamic.attributes.ControlEffectAttributes;
import de.lessvoid.nifty.controls.listbox.CreateListBoxControl;
import de.lessvoid.nifty.controls.listbox.controller.ListBoxControl;
import de.lessvoid.nifty.controls.textfield.controller.TextFieldControl;
import de.lessvoid.nifty.elements.Element;
import de.lessvoid.nifty.input.NiftyInputEvent;
import de.lessvoid.nifty.screen.Screen;
import de.lessvoid.nifty.screen.ScreenController;
import java.util.ArrayList;
import java.util.List;

public class StartGameManager implements ScreenController {

    private Screen screen;
    private Nifty nifty;
    private ListBoxControl dynamicListbox;
    private List<String> mapsList = new ArrayList<String>();

    private void initMaps() {
        for(int i = 1; i <= 4; i++) {
            mapsList.add("Map_" + i);
        }
    }

    public void bind(Nifty newNifty, Screen newScreen) {
        screen = newScreen;
        nifty = newNifty;

        initMaps(); //======== TODO ========== mapstardi tipa kosadi listke

        Element listBoxPanel = screen.findElementByName("maps-list");

         // create new panel for listbox
        PanelCreator createPanel = new PanelCreator();
        createPanel.setChildLayout("horizontal");
        createPanel.setHeight("*");
        Element row = createPanel.create(newNifty, screen, listBoxPanel);


        // create a list box
        CreateListBoxControl dynamicListboxCreate = new CreateListBoxControl("listBoxDynamic");
        dynamicListboxCreate.set("horizontal", "false");
        dynamicListboxCreate.setWidth("*");
        dynamicListboxCreate.setHeight("100%");
        dynamicListboxCreate.setChildLayout("vertical");
        dynamicListboxCreate.setVisibleToMouse("true");
        dynamicListbox = dynamicListboxCreate.create(nifty, screen, row);

        for(String map : mapsList)
            dynamicListbox.addItem(map);
        
        LabelCreator createLabel = new LabelCreator("");
        createLabel.setStyle("nifty-listbox-item");

        ControlEffectAttributes effectParam = new ControlEffectAttributes();
        effectParam.setName("updateScrollpanelPositionToDisplayElement");
        effectParam.setAttribute("target", "listBoxDynamic");
        effectParam.setAttribute("oneShot", "true");
        createLabel.addEffectsOnCustom(effectParam);

        Element listBoxDataParent = screen.findElementByName("listBoxDynamicData");
        dynamicListbox.addElement(createLabel.create(nifty, screen, listBoxDataParent));

        // select first element
        dynamicListbox.changeSelection(0);
    }

    //method of starting game
    public void startGame() {
        String serverName = screen.findControl("server_name", TextFieldControl.class).getText();
        String mapChosen = mapsList.get(dynamicListbox.getSelectedItemIndex());

        System.out.println("Server Name: " + serverName + "\nMap: " + mapChosen);
    }

    public void cancel() {
        nifty.fromXml("/src/test4/menus/main.xml", "start");
    }

    public void onStartScreen() {
       
    }

    public void onFocus(boolean bln) {
       
    }

    public void inputEvent(NiftyInputEvent nie) {
       
    }
    
    public void onEndScreen() {
        
    }
}
